/**
 * 2019年3月12日 下午1:21:30  
 * richest-api  
 * @author zhaohongchao
 * @version 1.0
 * UserServiceImpl.java  
 */
package com.richest.api.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.richest.api.controller.form.LoginForm;
import com.richest.api.controller.form.PhoneForm;
import com.richest.api.controller.form.RegisterForm;
import com.richest.api.dao.UserMapper;
import com.richest.api.service.IUserService;
import com.richest.api.utils.Constant;
import com.richest.api.utils.RedisUtils;
import com.richest.bean.UserBean;
import com.richest.util.CallResults;
import com.richest.util.DateUtils;
import com.richest.util.MD5Util;
import com.richest.util.StringUtil;
import com.richest.util.UUIDGenerator;

/**
 * 2019年3月12日 下午1:21:30 richest-api
 * 
 * @author zhaohongchao
 * @version 1.0 UserServiceImpl.java
 */
@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private RedisUtils redisUtils;

	@Override
	public UserBean queryByMobile(String phone) {
		return userMapper.queryByMobile(phone);
	}

	@Override
	public CallResults login(LoginForm form) {
		UserBean userBean = userMapper.queryByMobile(form.getPhone());
		// 验证用户
		if (null == userBean) {
			return CallResults.error("手机号错误");
		}
		// 验证密码
		if (!userBean.getPassword().equals(MD5Util.string2MD5(form.getPassword()))) {
			return CallResults.error("密码错误");
		}

		String token = UUIDGenerator.generatorUUID(String.valueOf(userBean.getUser_id()) + "-");
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		redisUtils.set("token" + userBean.getUser_id(), token, Constant.TOKEN_EXPIRE);
		map.put("user", userBean);

		return CallResults.ok().put("data", map);
	}

	@Override
	public CallResults register(RegisterForm form) {
		UserBean userBean = userMapper.queryByMobile(form.getPhone());
		if (null != userBean) {
			return CallResults.error("手机号已被注册");
		}

		userBean = new UserBean();
		userBean.setPhone(form.getPhone());
		userBean.setUser_name(form.getPhone());
		if (StringUtil.isNotNullOrEmpty(form.getPassword())) {
			userBean.setPassword(MD5Util.string2MD5(form.getPassword()));
		}
		userBean.setRegeist_time(DateUtils.getNowTime());
		userBean.setSource(Constant.USER_SOURCE_APP);
		userBean.setMethod(Constant.USER_METHOD_PHONE);
		userBean.setCreate_by(form.getPhone());
		userMapper.register(userBean);

		return CallResults.ok();
	}

	@Override
	public CallResults speedRegister(PhoneForm form) {
		UserBean userBean = userMapper.queryByMobile(form.getPhone());
		if (null != userBean) {
			String token = UUIDGenerator.generatorUUID(String.valueOf(userBean.getUser_id()) + "-");
			Map<String, Object> map = new HashMap<>();
			map.put("token", token);
			redisUtils.set("token" + userBean.getUser_id(), token, Constant.TOKEN_EXPIRE);

			return CallResults.ok().put("data", map);
		}

		userBean = new UserBean();
		userBean.setPhone(form.getPhone());
		userBean.setUser_name(form.getPhone());
		userBean.setRegeist_time(DateUtils.getNowTime());
		userBean.setSource(Constant.USER_SOURCE_APP);
		userBean.setMethod(Constant.USER_METHOD_PHONE);
		userBean.setCreate_by(form.getPhone());
		userBean.setCreate_by(form.getPhone());
		userMapper.speedRegister(userBean);

		String token = UUIDGenerator.generatorUUID(String.valueOf(userBean.getUser_id()) + "-");
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		redisUtils.set("token" + userBean.getUser_id(), token, Constant.TOKEN_EXPIRE);

		return CallResults.ok().put("data", map);
	}

	@Override
	public UserBean selectOne(UserBean userBean) {
		return userMapper.selectOne(userBean);
	}

	@Override
	public void update(UserBean userBean) {
		userMapper.update(userBean);
	}

	@Override
	public void insert(UserBean userBean) {
		userMapper.register(userBean);
	}
}
